package com.webshop.DataBase;

import com.webshop.JavaBean.Goods;
import com.webshop.JavaBean.Picture;
import com.webshop.service.PicuploadService;
import com.webshop.service.Upload;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/picload")//图片上传
public class PicuploadServelt extends javax.servlet.http.HttpServlet{
    SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy.MM.dd ");
    String currentDate =dateFormat.format( new Date() );
    PicuploadService picuploadService=new PicuploadService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        HttpSession session=req.getSession();
        Object obj = session.getAttribute("name");
        String adminname = String.valueOf(obj);
          Map<String,String> map=new HashMap<>();
        DiskFileItemFactory  fileItemFactory=new DiskFileItemFactory();
        ServletFileUpload servletFileUpload=new ServletFileUpload(fileItemFactory);
        try{
            List<FileItem> list=servletFileUpload.parseRequest(req);
            for(FileItem f:list){
                String name=f.getFieldName();
                if(f.isFormField()){
                    String vlaue=f.getString("UTF-8");
                    map.put(name,vlaue);
                }
                else {
                    String picname=f.getName();
                    //获取路径
                    String dangqianpath="D:\\apache-tomcat-9.0.7\\webapps\\picupload";
                    PicuploadService  pic=new PicuploadService();
                    //随机生成字母
                    String ss= pic.getPicname();
                    //保存的名字
                    String uploadname=ss+picname;
                    String dir=Upload.getDir();
                   File file=new File(dangqianpath,dir);
                    if (!file.exists()){
                        file.mkdirs();
                    }
                    InputStream inputStream=f.getInputStream();
                    //保存文件的真正位置
                   File file1=new File(file,uploadname);
                    FileOutputStream fileOutputStream=new FileOutputStream(file1);
                    IOUtils.copy(inputStream,fileOutputStream);
                    f.delete();
                    String picul="picupload"+dir+"/"+uploadname;
                    map.put(name, picul);
                }
            }
            //前面控件的name属性必须与Picture的属性一样否则查不了
            //上传的类型可以根据form表单更改
            Goods goods=new Goods();
            BeanUtils.populate(goods,map);
            goods.setCreateTime(currentDate);
            goods.setIsHot("1");
            goods.setState(1);
            goods.setMinprice(goods.getMaxprice());
            goods.setOwner("gao");
            goods.setCategory(1);
            System.out.println(goods);
            picuploadService.insertgood(goods);
            int id=picuploadService.selet(goods.getMainPic());
           Picture picture=new Picture();
           picture.setGoodsID(id);
           picture.setIsMain(0);
           picture.setPath(goods.getMainPic());
           picture.setGoodsAttrID(1);
            picuploadService.insert(picture);
        }catch (Exception e){
          System.out.println("flase");
        }
    }
}
